﻿using System;
using UnityEngine;
using UnityEngine.UI;

public class UIShopping : MonoBehaviour {

    //需要更换的图片
    public Image Hero;

    //英雄图片数组
    public Sprite[] imagearr;
    //图片数组的索引  
    private int i;

    //声明按钮引用
    public Button btn_left; 
    public Button btn_right;

    //解锁英雄按钮
    public Button btn_jiesuo;

    // Use this for initialization
    void Start () {
        //找到左边按钮并且获取其身上组件  
        btn_left = GameObject.Find("Left_Btn").GetComponent<Button>();
        //为onclick注册方法  
        btn_left.onClick.AddListener(SwapLeft);
        //右面同理
        btn_right = GameObject.Find("Right_Btn").GetComponent<Button>();
        btn_right.onClick.AddListener(SwapRight);

        btn_jiesuo = GameObject.Find("JieSuo_btn").GetComponent<Button>();
        btn_jiesuo.onClick.AddListener(JieSuo);
    }

    private void JieSuo()
    {
        Text text = GameObject.Find("JieSuo_btn/Text").GetComponent<Text>();
        if (PlayerMove.gold >= 25)
        {
            PlayerMove.gold -= 25;
            PlayerPrefs.SetInt("gold",PlayerMove.gold);
            text.text = "已解锁";
        }
        else
        {
            Debug.Log("TODO金币不足");
        }
    }

    public void SwapLeft()
    {

        //如果当前图片是图片数组的第一张，就切换到最后一张             
        if (i < 0) { i = imagearr.Length - 1; }
        //切换图片  
        Hero.overrideSprite = imagearr[i--];
    }
    public void SwapRight() { 

        if (i > imagearr.Length - 1) { i = 0; }
        //切换图片  
        Hero.overrideSprite = imagearr[i++];
    }

}
